function getdata(e) { //讀取ID歷史紀錄

	var personId = $("#personId").val();
	if(personId == "")
		return;
	else if (!checkID(personId)){
		$("#personId").val("");
		location.reload();
	}else{	
		$.ajax({
			type : "POST",
			url : "fooddemohistory.do",
			dataType : 'text',

			data : {
				Id : $("#personId").val()
			},

			error : function(xhr) {
				alert("Ajax request error!");
			},
			
			success : function(DataList) {
				var ss = DataList.split("$");
				for (var i = 0 ; i < ss.length ; i++){
					var str = "#r"+i;
					$(str).html(ss[i]);
				}				
				readsuggest();
			}
		});
		

	}
		
}

function readsuggest(){

	$.ajax({
		
		type : "POST",
		url : "readHistory.do",
		dataType : 'text',
		
		data : {
			type: "printdata",
			id : $("#personId").val()
		},
		
		error : function(xhr) {
			alert("Ajax request error!");
		},
		
		success : function(data) {

			var str = data.split("|");
			if (str[0] == "nodata") { //資料庫無此資料
				alert(str[1]);
				location.reload();
			} else {
				// sex radio
				if (str[0] == "male")
					$('input[name=sex]').get(0).checked = true;
				else if (str[0] == "female")
					$('input[name=sex]').get(1).checked = true;
				else
					$('input[name=sex]').attr("checked", false);
				// weightType
				if (str[1] == "old")
					$('input[name=weightType]').get(0).checked = true;
				else if (str[1] == "new")
					$('input[name=weightType]').get(1).checked = true;
				else
					$('input[name=weightType]').attr("checked", false);
				// work radio
				if (str[2] == "little")
					$('input[name=work]').get(0).checked = true;
				else if (str[2] == "middle")
					$('input[name=work]').get(1).checked = true;
				else if (str[2] == "large")
					$('input[name=work]').get(2).checked = true;
				else
					$('input[name=work]').attr("checked", false);
				// activity radio
				if (str[3] == "little")
					$('input[name=activity]').get(0).checked = true;
				else if (str[3] == "middle")
					$('input[name=activity]').get(1).checked = true;
				else if (str[3] == "large")
					$('input[name=activity]').get(2).checked = true;
				else
					$('input[name=activity]').attr("checked", false);
				// select data
				if (str[4]) {
					var getdata = str[4].split(";");
					for ( var i = 0; i < getdata.length; i++) {
						if (getdata[i])
							nourish_list(getdata[i]); // 營養品攝取份量計算
					}
				}
				// text data
				for ( var i = 5; i < str.length; i++) {
					var getdata = str[i].split(":");
					$(getdata[0]).val(getdata[1]);
				}
				//$("#age").html("test");

				get_sqldata($('input[name=sex]:checked').val(), 
						$('input[name=weightType]:checked').val(), 
						$('input[name=work]:checked').val(), 
						$('input[name=activity]:checked').val());

				Total_intake();
			}	
		}
		
	});
}

function nourish_list(e) {	//營養品攝取份量計算
	var str, add_prot=0, add_fat=0, add_cho=0, cal=0;

	$.ajax({
		success: function () {
			str = e.split(",");

			for ( var i = 2; i < str.length; i++) { 
				var a = str[i].split("：");
				if (a[0].match("蛋白質"))   //公克
					add_prot = parseFloat(a[1]);
				else if (a[0].match("脂肪"))   //公克
					add_fat = parseFloat(a[1]);
				else if (a[0].match("醣類"))  //公克
					add_cho = parseFloat(a[1]);	
			}
			
			var prot_g = parseFloat($("#prot_intake").html()) + parseFloat(add_prot);
			var fat_g = parseFloat($("#fat_intake").html()) + parseFloat(add_fat);
			var cho_g = parseFloat($("#cho_intake").html()) + parseFloat(add_cho);
			
			var prot_ = parseFloat(prot_g * 4);
			var fat_ = parseFloat(fat_g * 9);
			var cho_ = parseFloat(cho_g * 4);
			cal = prot_ + fat_ + cho_;
			
			$("#nourish_list").append($("<option></option>").attr("value", e).text(e));			
			$("#prot_intake").html(prot_g);			
			$("#prot_").html(prot_);
			$("#fat_intake").html(fat_g);			
			$("#fat_").html(fat_);
			$("#cho_intake").html(cho_g);
			$("#cho_").html(cho_);
			$("#calofday_intake").html(parseFloat(cal));

			Save_nourish_val("add", parseFloat(add_prot), parseFloat(add_fat), parseFloat(add_cho));
			drawChart(prot_, fat_, cho_, "chart1");  //繪製圓餅圖
			Compare();		//飲食建議與攝取比較
		}
	});
}

//-----------------------------------------------------------------------
$(document).ready(function(e) {
		
	$("#getdata").on("click",getdata); //讀取ID歷史紀錄
});